/**
 * <p>Title: ObstacleContainer.java</p>
 * <p>Description: CS 343 Final Project. This class contains the the obstacle container</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package Obstacles;

import java.util.ConcurrentModificationException;
import java.util.Vector;



public class ObstacleContainer {

	private Vector<Obstacle> obs;

	/**
	 * @param none
	 * @description creates the obstacle container to be used
	 */
	public ObstacleContainer() {
		obs = new Vector<Obstacle>();
	}

	/**
	 * @param x
	 * @description adds an obstacle to the container
	 */
	public synchronized void addObstacle(Obstacle x) {
		synchronized (obs) {
			obs.add(x);
		}
	}

	/**
	 * @param none
	 * @description gets an obstacle from the container
	 */
	public synchronized Vector<Obstacle> getObs() {
		synchronized (obs) {
			return obs;
		}
	}

	/**
	 * @param none
	 * @description clears the container of obstacles
	 */
	public synchronized void clearContainer() {
		synchronized (obs) {
			obs.clear();
		}
	}

	/**
	 * @param obs
	 * @description sets the obstacle container
	 */
	public synchronized void setObs(Vector<Obstacle> obs) {
		synchronized (obs) {
			this.obs = obs;
		}
	}

	/**
	 * @param x
	 * @description removes the obstacle from the collection
	 */
	public synchronized void removeObstacle(Obstacle x) {
		synchronized (obs) {
			obs.remove(x);
		}
	}

	/**
	 * @param none
	 * @description moves the obstacles
	 */
	public synchronized void moveObstacles() {
		Vector<Obstacle> stuffToRemove = new Vector<Obstacle>();
		if (getObs().isEmpty())
			return;
		for (Obstacle x : getObs()) {
			// checks to see if its way off to the left and on the ground, no
			// longer needed
			if (x.getYloc() > 200 && x.getXloc() < (-1) * x.getWidth()) {
				stuffToRemove.add(x);
			} else {
				// if its on the screen, move it. it not ^ remove it :P
				try {
					x.move();
				} catch (ConcurrentModificationException e) {
					System.err.println("GLITCH WHILE MOVING");
				}
			}
		}
		for (Obstacle x : stuffToRemove) {
			removeObstacle(x);
		}

	}

}
